package aufgabe56;

import java.util.*;
import javax.media.j3d.*;

import com.sun.j3d.utils.geometry.*;

public class CollisionBehaviour extends Behavior {

	private WakeupOnCollisionMovement hit;

	private boolean switchOff;
	private Primitive collidingShape;
	private Alpha movement;

	public CollisionBehaviour(Primitive theShape, Alpha alpha, Bounds bounds, boolean knopf) {
		this.collidingShape = theShape;
		this.movement = alpha;
		this.switchOff = knopf;

		setSchedulingBounds(bounds);
	}

	public void initialize() {
		hit = new WakeupOnCollisionMovement(collidingShape);
		wakeupOn(hit);
	}

	public void processStimulus(Enumeration criteria) {
		while (criteria.hasMoreElements()) {
			WakeupCriterion theCriterion = (WakeupCriterion) criteria.nextElement();
			
			if (theCriterion instanceof WakeupOnCollisionMovement) {
				System.out.println("Kollision");
				movement.setStartTime(Long.MAX_VALUE);

				if (switchOff && Taschenlampe.bgLicht.getAllChildren().hasMoreElements()) {
					Taschenlampe.bgLicht.detach();
					Taschenlampe.bgLicht.removeChild(0);	
				}
				
				Enumeration e = ((WakeupOnCollisionMovement) theCriterion).triggeredElements();

				while (e.hasMoreElements()) {
					e.nextElement();
				}
			}
			wakeupOn(hit);
		}
	}
}
